﻿using System.IO;

namespace LS.Screening.UI.Common
{
    public static class StreamExtensions
    {
        /// <summary>
        /// 从流中读取字节数据
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static byte[] ReadData(this Stream stream)
        {
            if (stream.CanSeek)
            {
                stream.Position = 0L;
            }
            List<byte> list = new List<byte>();
            byte[] array = new byte[1024];
            while (true)
            {
                int num = stream.Read(array, 0, array.Length);
                if (num <= 0)
                {
                    break;
                }
                list.AddRange(array.Take(num));
            }
            return list.ToArray();
        }

        /// <summary>
        /// 从流中读取字节数据
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static async Task<byte[]> ReadDataAsync(Stream stream)
        {
            if (stream.CanSeek)
            {
                stream.Position = 0L;
            }
            List<byte> list = new List<byte>();
            byte[] array = new byte[1024];
            while (true)
            {
                int num = await stream.ReadAsync(array, 0, array.Length);
                if (num <= 0)
                {
                    break;
                }
                list.AddRange(array.Take(num));
            }
            return list.ToArray();
        }
    }
}
